package com.tesco.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author al
 * @date 2020/7/2 9:02
 * @description
 */
@Data
@ApiModel("API通用相应信息实体")
public class WebApiResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "接口相应状态：true - 成功， false - 失败")
    private Boolean success = true;

    @ApiModelProperty(value = "返回的数据实体")
    private T data;

    @ApiModelProperty(value = "接口提示信息")
    private String msg;

    @ApiModelProperty(value = "状态码")
    private int respCode;

    @ApiModelProperty(value = "数据条数")
    private Integer count;

    public WebApiResult() {}

    public WebApiResult(int respCode,boolean success, String msg,T data,int count) {
        this.success = success;
        this.data = data;
        this.msg=msg;
        this.respCode=respCode;
        this.count=count;
    }

    public static <T> WebApiResult<T> ok() {
        return new WebApiResult<>(200, true, "success",  null,0);
    }

    public static <T> WebApiResult<T> ok(T data) {
        return new WebApiResult<>(200, true, "success",  data,0);
    }

    public static <T> WebApiResult<T> ok(T data, String message) {
        return new WebApiResult<>(200, true, message,  data,0);
    }

    public static <T> WebApiResult<T> ok(T data, int count) {
        return new WebApiResult<>(200, true, "查询成功！", data,count);
    }

    public static <T> WebApiResult<T> ok(String message) {
        return new WebApiResult<>(200, true, message,null,0);
    }

    public static <T> WebApiResult<T> error(Exception ex) {
        String exceptionMessageFormat = "Message: %s, StackTrace: %s, Suppressed: %s, Cause: %s, Class: %s %s";

        String msg = String.format(exceptionMessageFormat, ex.getMessage(), ex.getStackTrace(), ex.getSuppressed(),
                ex.getCause(), ex.getClass(), System.getProperty("line.separator"));
        return error(msg);
    }

    public static <T> WebApiResult<T> error() {
        return new WebApiResult<T>(500, false, "接口调用失败", null, 0);
    }

    public static <T> WebApiResult<T> error(String message) {
        return new WebApiResult<T>(500, false, message, null, 0);
    }

    public static <T> WebApiResult<T> error(int code, String message) {
        return new WebApiResult<T>(code, false, message, null, 0);
    }

}
